package com.eattravel.controller;

import com.eattravel.pojo.Commodity;
import com.eattravel.service.CommodityService;
import com.eattravel.vo.AreaFood;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author glong
 * @date 2019/9/23 21:07
 * 描述：查询关于区域结果的显示
 */
@RestController
@RequestMapping("/area")
public class AreaFoodController {

    public static void main(String[] args) {

    }
    @Autowired
    private CommodityService commodityService;

    String area;

    @RequestMapping("setArea")
    public void setTheArea(String theArea){
        area = theArea;
    }

    /**
     * 地方食物，模糊搜索
     * @return map集合
     */
    @RequestMapping("/areaFood1")
    @ResponseBody
    public Map<String ,Object> getAreaFood1(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByArea(area);
            if (list != null) {
                // 转化为vo
                for (int i = 0; i < list.size(); i++) {
                    AreaFood food = new AreaFood();
                    food.setCommodityId(list.get(i).getCommodityId());
                    food.setCommodityName(list.get(i).getCommodityName());
                    food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                    food.setPrice(list.get(i).getCommodityPrice());
                    food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                    areaFoods.add(food);
                }
                modelMap.put("areaFood", areaFoods);
                modelMap.put("total", areaFoods.size());
            } else {

            }
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("areaFood2")
    @ResponseBody
    public Map<String ,Object> getAreaFood2(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByArea2(area);
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
            modelMap.put("areaFood", areaFoods);
            modelMap.put("total",areaFoods.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("areaFood3")
    @ResponseBody
    public Map<String ,Object> getAreaFood3(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByArea3(area);
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
            modelMap.put("areaFood", areaFoods);
            modelMap.put("total",areaFoods.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("areaFood4")
    @ResponseBody
    public Map<String ,Object> getAreaFood4(){
        Map<String ,Object> modelMap = new HashMap<String ,Object>();
        List<AreaFood> areaFoods = new ArrayList<>();
        // 取出查询到的数据(食品类)
        List<Commodity> list = null;
        try {
            list = commodityService.getFoodByArea4(area);
            // 转化为vo
            for (int i=0; i<list.size(); i++){
                AreaFood food = new AreaFood();
                food.setCommodityId(list.get(i).getCommodityId());
                food.setCommodityName(list.get(i).getCommodityName());
                food.setCommodityDescribe(list.get(i).getCommodityDescribe());
                food.setPrice(list.get(i).getCommodityPrice());
                food.setPicture(list.get(i).getPictureList().get(0).getPicture());
                areaFoods.add(food);
            }
            modelMap.put("areaFood", areaFoods);
            modelMap.put("total",areaFoods.size());
        } catch (Exception e){
            e.printStackTrace();
            modelMap.put("success",true);
            modelMap.put("errMsg",e.toString());
        }
        return modelMap;
    }

    @RequestMapping("findArea")
    @ResponseBody
    public String findArea(HttpSession session){
        String area = (String) session.getAttribute("area");

        return area;
    }

}
